package jsyf.oa.dao.email;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.constant.Const;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.entity.attach.AttachmentEntity;
import jsyf.oa.entity.email.EmailEntity;
import jsyf.oa.entity.email.EmailMainEntity;
import jsyf.oa.entity.email.EmailReceiverEntity;
import jsyf.oa.entity.email.InboxEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by y on 2018/3/7.
 */
@Repository
public class DraftsDao extends BaseDao {


    public DraftsDao() {
        super.iBaseEntity = new EmailMainEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, EmailMainEntity.class);
    }
    public List<JSONObject> getByEmailReceiver(String emailId, String userType) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" receiver.USER_ID AS userId, \n");
        sql.append(" receiver.USER_NAME AS userName\n");
        sql.append(" FROM \n");
        sql.append(" T_SYS_EMAIL_RECEIVER AS receiver \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(" AND receiver.EMAIL_ID = ? AND receiver.USER_TYPE = ? \n");
        Object[] params = {emailId, userType};
        // 查询结果
        List<JSONObject> listResult = super.queryForJsonList(sql.toString(), params);
        return listResult;
    }
    public List<JSONObject> findByAttaches(String emailId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" attach.ID AS id, \n");
        sql.append(" attach.FILE_NAME AS name, \n");
        sql.append(" attach.FILE_URL AS url \n");
        sql.append(" FROM \n");
        sql.append(" T_ATTACH_MODULE AS attach_module \n");
        sql.append(" LEFT JOIN T_ATTACHMENT AS attach ON attach_module.ATTACH_ID = attach.ID \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(" AND attach_module.MODULE_ID = ? \n");
        Object[] params = {emailId};

        // 查询结果
        List<JSONObject> listResult = super.queryForJsonList(sql.toString(), params);


        return listResult;
    }
}
